import ObjectUtil from "./ObjectUtil";

export default class LogUtil {

    private static ShowStackAndTime: boolean = false;
    private static isDebug: boolean = true;
    
    public static log(...args) {
        if(!this.isDebug) return;
        if(cc.sys.isNative && ObjectUtil.isObject(args)){
            console.log(this.dump(args));
            return;
        }
        var backLog = console.log || cc.log
        let arr: Array<any> = Array.prototype.slice.call(arguments);
        this.addStackAndTime(arr);
        backLog.apply(backLog, arr);
    }

    public static warn(...args) {
        if(!this.isDebug) return;
        var backLog = console.warn || cc.warn
        let arr: Array<any> = Array.prototype.slice.call(arguments);
        this.addStackAndTime(arr);
        backLog.apply(backLog, arr);
    }

    public static error(...args) {
        if(!this.isDebug) return;
        var backLog = console.error || cc.error
        let arr: Array<any> = Array.prototype.slice.call(arguments);
        this.addStackAndTime(arr);
        backLog.apply(backLog, arr);
    }

    private static addStackAndTime(arr) {
        if (this.ShowStackAndTime) {
            let info = LogUtil.stack(2) + LogUtil.getDateString() + " ";
            arr.splice(0, 0, info);
        }
    }

    private static getDateString(): string {
        let d = new Date();
        let str = d.getHours().toString();
        let timeStr = "";
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMinutes().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getSeconds().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMilliseconds().toString();
        if (str.length == 1) str = "00" + str;
        if (str.length == 2) str = "0" + str;
        timeStr += str;

        timeStr = "[" + timeStr + "]";
        return timeStr;
    }

    private static stack(index = 2): string {
        var e = new Error();
        var lines = e.stack.split("\n");
        lines.shift();
        var result = [];
        lines.forEach(function (line) {
            line = line.substring(7);
            var lineBreak = line.split(" ");
            if (lineBreak.length < 2) {
                result.push(lineBreak[0]);
            } else {
                result.push({ [lineBreak[0]]: lineBreak[1] });
            }
        });

        var list = [];
        if (index < result.length - 1) {
            for (var a in result[index]) {
                list.push(a);
            }
        }

        if(list.length == 0) return "";

        var splitList = list[0].split(".");
        if(splitList.length == 0) return "";
        if(splitList.length == 1) return splitList[0] + ".ts:";
        
        return (splitList[0] + ".ts->" + splitList[1] + ":");
    }

    public static getFileName(): string {
        if(!this.isDebug) return "";
        return LogUtil.stack(2);
    }

    public static dump(args): string{
        return JSON.stringify(args);
    }

}